package com.jb.repair.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jb.repair.F1Application;
import com.jb.repair.R;
import com.jb.repair.common.Constants;
import com.jb.repair.model.RegularTaskModel;
import com.jb.repair.model.RegularWorkModel;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by wp1988 on 2016/7/18 0018.
 */
public class RegularTaskAdapter extends BaseAdapter {

    private List<RegularWorkModel> mdata;
    private Context mcontext;
    private LayoutInflater mInflater;
    private static final String TAG = "RegularTaskAdapter";

    public RegularTaskAdapter(Context mContext, List<RegularWorkModel> mdata) {
        this.mcontext = mContext;
        this.mdata = mdata;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.layout_item_regular_task, null);
            viewHolder = new ViewHolder();
            viewHolder.tvIndex = (TextView)convertView.findViewById(R.id.tv_index);
            viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name);
            viewHolder.tvCompleteDate = (TextView)convertView.findViewById(R.id.tv_complete_time);
            viewHolder.tvManager = (TextView)convertView.findViewById(R.id.tv_manager);
            viewHolder.tvStatus = (TextView)convertView.findViewById(R.id.tv_status);
            viewHolder.tvRegisterDate = (TextView)convertView.findViewById(R.id.tv_register_date);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tvIndex.setText(position + 1 + "");
        viewHolder.tvName.setText(mdata.get(position).TASK_NAME);
        viewHolder.tvCompleteDate.setText(mdata.get(position).SHOULD_FINISH_TIME);
        viewHolder.tvManager.setText(F1Application.getUserName() == null ? "未知用户" : F1Application.getUserName());
        try {
            JSONObject jsonObject = new JSONObject(Constants.CT_TASK_STATUS);
            viewHolder.tvStatus.setText(jsonObject.getString(mdata.get(position).TASK_STATUS));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        viewHolder.tvStatus.setTextColor(mcontext.getResources().getColor(mdata.get(position).TASK_STATUS.equals("0700202") ? R.color.green_dark : R.color.red));
        String date = mdata.get(position).update_time;
//        Log.i(TAG, "date = "+date);
        int index = date.indexOf(' ');
        if (index > 0) {
            date = date.substring(0, index) + "\n" + date.substring(index);
        }
        viewHolder.tvRegisterDate.setText(date);

        return convertView;
    }

    class ViewHolder{
        TextView tvIndex;
        TextView tvName;
        TextView tvCompleteDate;
        TextView tvManager;
        //        TextView tvAttachNum;
        TextView tvStatus;
        TextView tvRegisterDate;
    }
}
